package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwYszzDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwYszz;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwYszzService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwYszzForm;;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwYszzServiceImpl  implements IYwHwYszzService{
	@Autowired
	private YwHwYszzDao ywHwYszzDao;
	
	/**
	 * 获取form
	 */
	public YwHwYszzForm getForm(Long Id) {
		YwHwYszz entity=ywHwYszzDao.get(Id);
		YwHwYszzForm form=new DozerBeanMapper().map(entity,YwHwYszzForm.class);
		return form;
	}

	public List<YwHwYszzForm> getListByQyId(Long qyId) {
		List<YwHwYszz> list=this.getListByQyid(qyId);
		List<YwHwYszzForm> forms=new ArrayList<YwHwYszzForm>();
		for(YwHwYszz entity:list){
			YwHwYszzForm form=new DozerBeanMapper().map(entity,YwHwYszzForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据ywid查询数据
	 * @param QYId
	 * @return
	 */
	private List<YwHwYszz> getListByQyid(Long qyId){
		String sql=" from "+YwHwYszz.class.getName()+" where  qyid="+qyId;
		List<YwHwYszz> list=this.ywHwYszzDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwYszzForm form) {
		YwHwYszz entity=new DozerBeanMapper().map(form,YwHwYszz.class);
		this.ywHwYszzDao.save(entity);
	}

	public void SaveOrUpdate(YwHwYszz entity) {
		this.ywHwYszzDao.save(entity);
	}

	public void parseYwHwYszz(HSSFSheet hssfsheet,Long qyid,Long ywid, String templateCode){
		String hql = "from YwHwYszz t where t.ywid = '"+ywid+"' and t.tplcode= '"+templateCode+"'";
		List<YwHwYszz> list = new ArrayList<YwHwYszz>();
		list = this.ywHwYszzDao.find(hql, list);
		for(int i=0; i<list.size();i++){
			this.ywHwYszzDao.delete(list.get(i));
		}
		try {
			
			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			System.out.println(hssfsheet.getPhysicalNumberOfRows());
			for (int i = 2; i < hssfsheet.getPhysicalNumberOfRows()-1; i++) {
				String zzlx;
				String sl;
				String mbqk;
				hssfrow = hssfsheet.getRow(i);
				// 判断是否还存在需要导入的数据
				if (hssfrow != null) {
					if (hssfrow.getCell((short) 0) == null) {
						zzlx = "";
					} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
						zzlx = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						zzlx = hssfrow.getCell((short) 0).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 1) == null) {
						sl = "";
					} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
						sl = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						sl = hssfrow.getCell((short) 1).getStringCellValue().trim();
					}
					if (hssfrow.getCell((short) 2) == null) {
						mbqk = "";
					} else if (hssfrow.getCell((short) 2).getCellType() == 0) {
						mbqk = new Double(hssfrow.getCell((short) 2).getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						mbqk = hssfrow.getCell((short) 2).getStringCellValue().trim();
					}
					if(StringUtils.isEmpty(zzlx)){
					}else{
							YwHwYszz ywHwYszz = new YwHwYszz();
							ywHwYszz.setQyid(qyid);
							ywHwYszz.setYwid(ywid);
							ywHwYszz.setSl(sl);
							ywHwYszz.setZzlx(zzlx);
							ywHwYszz.setMbqk(mbqk);
							ywHwYszz.setTplcode(templateCode);
							this.SaveOrUpdate(ywHwYszz);
					}
				}
					
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
